<?php

namespace app\v1\controller;

use think\Controller;
use think\Request;
use think\Validate;
use think\Weixin;

/**
 * Class Login
 * @package app\v1\controller
 * 登录模块控制器
 */
class Login extends Controller
{
    /**
     * 登录接口
     *
     */
    public function checkLogin()
    {
        $username=input('userName',null);
        $password=input('password',null);
        $validate=new Validate([
            'userName'=>'require|min:3|max:12',
            'password'=>'require|min:6|max:12'
        ]);
        $validate->message([
            'userName.require'=>'账户名不能为空',
            'userName.min'=>'账户名请填写3-12位字符',
            'userName.max'=>'账户名请填写3-12位字符',
            'password.min'=>'密码请填写3-12位字符',
            'password.max'=>'密码请填写3-12位字符',
            'password.require'=>'密码不能为空',

        ]);
        $data=$this->request->post();
        if(!$validate->check($data)){
            renderInfo(400001,$validate->getError());
        }
        $user=model('v1/User');
        $user->checkLogin($username,$password);
    }

    //微信授权接口
    public function weixinAuth(){
<<<<<<< Updated upstream
        $weixin=new Weixin(config('wechat_appid'),config('wechat_appsecret'));
=======
        $weixin=new Weixin();
		//echo $weixin->appid;
		//die;
        $host='dev.open.weixin.haotuoguan.cn';
>>>>>>> Stashed changes
        if(empty($_GET['code'])){

            header('Location: https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$weixin->appid.'&redirect_uri=http%3a%2f%2f'.$_SERVER['HTTP_HOST'].'%2fv1%2fLogin%2fweixinAuth&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect');
            exit();
        }
        $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$weixin->appid."&secret=".$weixin->appsecret."&code=".$_GET['code']."&grant_type=authorization_code";

        $res = $weixin->https_request($url);//调用SDK方法获取到res 从中可以得到openid

        $res=(json_decode($res, true));
        $user=model('v1/User');
       // $webUrl='http://192.168.1.115:8080/enter';
      //$webUrl="http://dev.h5.open.weixin.haotuoguan.cn/enter";
      $webUrl=config('web_url').'/enter';
//        dump($res['openid']);
//        die;
        $result=$user->authMakeLogin($res['openid']);
        if(!empty($result)){
            $this->redirect($webUrl.'?state=1&parent_token='.$result['parent_token'].'&parentId='.$result['parentId'].'&userId='.$result['childId']);
        }else{
            $this->redirect($webUrl.'?state=0&code='.$res['openid']);
        }


    }

    //获取微信的信息
  /* public function getAuthInfo(){
        $weixin=new Weixin();
        $code=input('code',null);
        $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$weixin->appid."&secret=".$weixin->appsecret."&code=".$code."&grant_type=authorization_code";

        $res = $weixin->https_request($url);//调用SDK方法获取到res 从中可以得到openid

        $res=(json_decode($res, true));
        dump($res);
        die;
    }*/

  //通过openid自动登录并且返回登录Id和token
    public function getLoginInfo(){

    }
    //找回密码
    public function getForgetPassword(){
        $phone=input('phone',null);
        $verifyCode=input('verifyCode',null);
        $newPassword=input('newPassword',null);

        $validate=new Validate([
            'phone'=>'require|length:11',
            'verifyCode'=>'require|length:6',
            'newPassword'=>'require|min:6|max:18'

        ]);
        $validate->message([
            'phone.require'=>'请填写手机号码',
            'phone.length'=>'请填写正确的手机号号码',
            'verifyCode.require'=>'请输入验证码',
            'verifyCode.length'=>'请输入6位验证码',
            'newPassword.min'=>'密码至少大于6位字符',
            'newPassword.max'=>'密码不大于18位字符'

        ]);
        $data=$this->request->post();
        if(!$validate->check($data)){
            renderInfo(400001,$validate->getError());
        }
        $isPhone=isPhone($phone);

        if(!$isPhone){
            renderInfo(400001,'请填写正确的手机号号码');
        }
        $user=model('v1/User');
        $user->getForgetPassword($verifyCode,$phone,$newPassword);
    }
    //验证码登录
    public function verifyCodeLogin(){
        $phone=input('phone',null);
        $verifyCode=input('verifyCode',null);

        $validate=new Validate([
            'phone'=>'require|length:11',
            'verifyCode'=>'require|length:6'

        ]);
        $validate->message([
            'phone.require'=>'请填写手机号码',
            'phone.length'=>'请填写正确的手机号号码',
            'verifyCode.require'=>'请输入验证码',
            'verifyCode.length'=>'请输入6位验证码'


        ]);
        $data=$this->request->post();
        if(!$validate->check($data)){
            renderInfo(400001,$validate->getError());
        }
        $isPhone=isPhone($phone);

        if(!$isPhone){
            renderInfo(400001,'请填写正确的手机号号码');
        }
        $user=model('v1/User');
        $user->verifyCodeLogin($phone,$verifyCode);
    }






}
